{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入常用的库\n",
    "import os\n",
    "import cv2\n",
    "# 导入MindVision品牌的相机库mvsdk\n",
    "import mvsdk\n",
    "\n",
    "# 相机持续拍摄并保存照片\n",
    "def get_capturedImage(imageFilePath=None):\n",
    "    if not imageFilePath:\n",
    "        imageFilePath = '../resources/temp.jpg'\n",
    "    device_list = mvsdk.CameraEnumerateDevice()\n",
    "    if len(device_list) == 0:\n",
    "        return None\n",
    "    device_info = device_list[0]\n",
    "    cameraIndex = mvsdk.CameraInit(device_info, -1, -1)\n",
    "    capability = mvsdk.CameraGetCapability(cameraIndex)\n",
    "    mvsdk.CameraSetTriggerMode(cameraIndex, 0)\n",
    "    # 加载相机配置文件\n",
    "    configFilePath = '../resources/camera.Config'\n",
    "    assert os.path.exists(configFilePath), 'please check if exists %s'%configFilePath\n",
    "    mvsdk.CameraReadParameterFromFile(cameraIndex, configFilePath)\n",
    "    # 获取相机拍摄照片的预处理\n",
    "    mvsdk.CameraPlay(cameraIndex)\n",
    "    FrameBufferSize = capability.sResolutionRange.iWidthMax * capability.sResolutionRange.iHeightMax * 3\n",
    "    FrameBuffer_address = mvsdk.CameraAlignMalloc(FrameBufferSize, 16)\n",
    "    RawData, FrameHead = mvsdk.CameraGetImageBuffer(cameraIndex, 2000)\n",
    "    mvsdk.CameraImageProcess(cameraIndex, RawData, FrameBuffer_address, FrameHead)\n",
    "    mvsdk.CameraReleaseImageBuffer(cameraIndex, RawData)\n",
    "    # 把文件路径转换为绝对文件路径\n",
    "    imageFilePath_1 = os.path.abspath(imageFilePath)\n",
    "    status = mvsdk.CameraSaveImage(cameraIndex, imageFilePath_1, FrameBuffer_address, FrameHead, mvsdk.FILE_JPG, 100)\n",
    "    if status != mvsdk.CAMERA_STATUS_SUCCESS:\n",
    "        print('ID为%d的相机拍摄并保存照片失败!!!')\n",
    "        is_successful = False\n",
    "    else:\n",
    "        print('ID为%d的相机保存照片至路径：%s' %(cameraIndex, imageFilePath_1))\n",
    "        is_successful = True\n",
    "    # 关闭相机、释放帧缓存\n",
    "    mvsdk.CameraUnInit(cameraIndex)\n",
    "    mvsdk.CameraAlignFree(FrameBuffer_address)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ID为3的相机保存照片至路径：C:\\Users\\Administrator\\Desktop\\23_git\\AI_Projects\\006_检测移动物体\\resources\\1.jpg\n"
     ]
    }
   ],
   "source": [
    "get_capturedImage('../resources/1.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ID为4的相机保存照片至路径：C:\\Users\\Administrator\\Desktop\\23_git\\AI_Projects\\006_检测移动物体\\resources\\2.jpg\n"
     ]
    }
   ],
   "source": [
    "get_capturedImage('../resources/2.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
